package com.ita.redis;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ita.dao.MerchantDao;
import com.ita.dao.impl.MerchantDaoImpl;
import com.ita.pojo.Merchant;

public class MerchantBlockFactory {

	private volatile static Map<String, Merchant> map;

	private MerchantBlockFactory() {

	}

	public static Map<String, Merchant> getStateMap() {
		return map;
	}

	public static void updateMerchant(String id, Merchant m) {

		map.remove(id);
		map.put(id, m);
	}

	public static void initBlockMap() {

		try {
			map = new ConcurrentHashMap<String, Merchant>();
			MerchantDao mDao = new MerchantDaoImpl();
			List<Merchant> list = mDao.findAll();
			if (list != null) {
				for (Merchant merchant : list) {
					map.put(merchant.getId(), merchant);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static Merchant getMerchant(String id) {

		if (map.containsKey(id)) {
			return map.get(id);
		} else {
			return null;
		}

	}

}
